package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.Hutool;
import cn.hutool.core.lang.Assert;
import com.ruoyi.system.mapper.ClassManageMapper;
import com.ruoyi.system.mapper.StudentManageMapper;
import com.ruoyi.system.mapper.TraceHistoryMapper;
import com.ruoyi.system.pojo.domain.ClassManage;
import com.ruoyi.system.pojo.domain.StudentManage;
import com.ruoyi.system.pojo.domain.TraceHistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TeachTraceMapper;
import com.ruoyi.system.pojo.domain.TeachTrace;
import com.ruoyi.system.service.ITeachTraceService;

/**
 * 教学追踪Service业务层处理
 *
 * @author sxb
 * @date 2024-01-22
 */
@Service
public class TeachTraceServiceImpl implements ITeachTraceService {
    @Autowired
    private TeachTraceMapper teachTraceMapper;
    @Autowired
    private TraceHistoryMapper traceHistoryMapper;
    @Autowired
    private ClassManageMapper classManageMapper;
    @Autowired
    private StudentManageMapper studentManageMapper;

    /**
     * 查询教学追踪
     *
     * @param id 教学追踪主键
     * @return 教学追踪
     */
    @Override
    public TeachTrace selectTeachTraceById(Long id) {
        return teachTraceMapper.selectTeachTraceById(id);
    }

    /**
     * 查询教学追踪列表
     *
     * @param teachTrace 教学追踪
     * @return 教学追踪
     */
    @Override
    public List<TeachTrace> selectTeachTraceList(TeachTrace teachTrace) {
        return teachTraceMapper.selectTeachTraceList(teachTrace);
    }

    /**
     * 新增教学追踪
     *
     * @param teachTrace 教学追踪
     * @return 结果
     */
    @Override
    public int insertTeachTrace(TeachTrace teachTrace) {
        //再添加逻辑中设置追踪的此时和负责人的名字(老师的名字)
        ClassManage classManage = classManageMapper.selectClassManageByClassName(teachTrace.getClassName());
        StudentManage studentManage =  studentManageMapper.selectstudentManageByStudentName(teachTrace.getStudentName());
        teachTrace.setTraceTimes(1L);
        teachTrace.setTraceTeacher("李某某");
        teachTrace.setClassId(classManage.getId());
        teachTrace.setStudentId(studentManage.getId());
        //在插入教学追踪的同时，需要同时插入到追踪历史中
        TraceHistory traceHistory = new TraceHistory();
        traceHistory.setStudentId(teachTrace.getStudentId());
        traceHistory.setStudentName(teachTrace.getStudentName());
        traceHistory.setTraceTime(teachTrace.getTraceTime());
        traceHistory.setTeacher(teachTrace.getTraceTeacher());
        traceHistory.setDescription(teachTrace.getDescrition());
        traceHistoryMapper.insertTraceHistory(traceHistory);

        return teachTraceMapper.insertTeachTrace(teachTrace);
    }

    /**
     * 修改教学追踪
     *
     * @param teachTrace 教学追踪
     * @return 结果
     */
    @Override
    public int updateTeachTrace(TeachTrace teachTrace) {
        //当我们选择更新这条追踪的时候，
        Long traceTimes = teachTrace.getTraceTimes();
        teachTrace.setTraceTimes(++traceTimes);

        TraceHistory traceHistory = new TraceHistory();
        traceHistory.setStudentId(teachTrace.getStudentId());
        traceHistory.setStudentName(teachTrace.getStudentName());
        traceHistory.setTraceTime(teachTrace.getTraceTime());
        traceHistory.setTeacher(teachTrace.getTraceTeacher());
        traceHistory.setDescription(teachTrace.getDescrition());
        traceHistoryMapper.insertTraceHistory(traceHistory);
        return teachTraceMapper.updateTeachTrace(teachTrace);
    }

    /**
     * 批量删除教学追踪
     *
     * @param ids 需要删除的教学追踪主键
     * @return 结果
     */
    @Override
    public int deleteTeachTraceByIds(Long[] ids) {
        return teachTraceMapper.deleteTeachTraceByIds(ids);
    }

    /**
     * 删除教学追踪信息
     *
     * @param id 教学追踪主键
     * @return 结果
     */
    @Override
    public int deleteTeachTraceById(Long id) {
        return teachTraceMapper.deleteTeachTraceById(id);
    }

    @Override
    public List<TeachTrace> selectTeachTracesById(Long id) {
        return teachTraceMapper.selectTeachTracesById(id);
    }
}
